package com.example.oasystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.oasystem.dao.burse.AoaBursementDaoOfJian;
import com.example.oasystem.dao.detailsburse.AoaDetailsburseDaoOfJian;
import com.example.oasystem.dao.evec.AoaEvectionDaoOfJian;
import com.example.oasystem.dao.holiday.AoaHolidayDaoOfJian;
import com.example.oasystem.dao.login.AoaUserDao;
import com.example.oasystem.dao.over.AoaOvertimeDaoOfJian;
import com.example.oasystem.dao.process.AoaProcessListDaoOfJian;
import com.example.oasystem.dao.regular.AoaRegularDaoOfJian;
import com.example.oasystem.dao.resign.AoaResignDaoOfJian;
import com.example.oasystem.dao.review.AoaReviewedDaoOfJian;
import com.example.oasystem.entity.burse.AoaBursementOfJian;
import com.example.oasystem.entity.detailsburse.AoaDetailsburseOfJian;
import com.example.oasystem.entity.evec.AoaEvectionOfJian;
import com.example.oasystem.entity.holiday.AoaHolidayOfJian;
import com.example.oasystem.entity.over.AoaOvertimeOfJian;
import com.example.oasystem.entity.process.AoaProcessListOfJian;
import com.example.oasystem.entity.regular.AoaRegularOfJian;
import com.example.oasystem.entity.resign.AoaResignOfJian;
import com.example.oasystem.entity.review.AoaReviewedOfJian;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaBursementServiceOfJian;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class AoaBursementServiceImplOfJian implements AoaBursementServiceOfJian {

    @Autowired
    private AoaProcessListDaoOfJian aoaProcessListDaoOfJian;
    @Autowired
    private AoaUserDao aoaUserDao;
    @Autowired
    private AoaBursementDaoOfJian aoaBursementDaoOfJian;
    @Autowired
    private AoaDetailsburseDaoOfJian aoaDetailsburseDaoOfJian;
    @Autowired
    private AoaEvectionDaoOfJian aoaEvectionDaoOfJian;
    @Autowired
    private AoaOvertimeDaoOfJian aoaOvertimeDaoOfJian;
    @Autowired
    private AoaRegularDaoOfJian aoaRegularDaoOfJian;
    @Autowired
    private AoaReviewedDaoOfJian aoaReviewedDaoOfJian;
    @Autowired
    private AoaHolidayDaoOfJian aoaHolidayDaoOfJian;
    @Autowired
    private AoaResignDaoOfJian aoaResignDaoOfJian;
    @Override
    public void insert(AoaBursementOfJian aoaBursementOfJian, long proFileId, Long userId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //在总表添加记录
        //在费用报销表添加记录
        //在费用列表添加记录
        AoaProcessListOfJian processId = aoaBursementOfJian.getProcessId();
        processId.setApplyTime(sdf.format(new Date()));
        processId.setStatusId(23L);
        processId.setTypeName("费用报销");
        if(proFileId != 0L){
            processId.setProFileId(proFileId);
        }
        processId.setProcessUserId(userId);
        processId.setIsChecked(0);
        processId.setShenuser(aoaBursementOfJian.getUsernameshen());
        System.out.println("费用报销"+processId);
        aoaProcessListDaoOfJian.insert(processId);
        System.out.println("1111成功");
        //审核表添加字段
        AoaReviewedOfJian aoaReviewedOfJian = new AoaReviewedOfJian();
        aoaReviewedOfJian.setStatusId(23L);
        aoaReviewedOfJian.setDel(0);
        aoaReviewedOfJian.setProId(processId.getProcessId());
        String[] split = aoaBursementOfJian.getUsernameshen().split(";");
        for (String s : split) {
            aoaReviewedOfJian.setUserId(aoaUserDao.selectOne(new QueryWrapper<AoaUser>().eq("user_name",s)).getUserId());
            aoaReviewedDaoOfJian.insert(aoaReviewedOfJian);
        }
        //aoaReviewedOfJian.setUserId(aoaUserDao.selectOne(new QueryWrapper<AoaUser>().eq("user_name",)).getUserId());
        //费用报销表添加字段
        double money = 0;
        int count = 0;
        for (AoaDetailsburseOfJian detail : aoaBursementOfJian.getDetails()) {
            count+=detail.getInvoices();
            money+=detail.getDetailmoney();
        }
        aoaBursementOfJian.setAllMoney(money);
        aoaBursementOfJian.setAllinvoices(count);
        aoaBursementOfJian.setProId(processId.getProcessId());
        System.out.println(aoaBursementOfJian.getNamemoney());
        aoaBursementOfJian.setUserName(aoaUserDao.selectOne(new QueryWrapper<AoaUser>().eq("user_name",aoaBursementOfJian.getNamemoney())).getUserId());
        aoaBursementDaoOfJian.insert(aoaBursementOfJian);
        System.out.println("2222成功");
        //在费用列表添加记录
        for (AoaDetailsburseOfJian detail : aoaBursementOfJian.getDetails()) {
            detail.setBursmentId(aoaBursementOfJian.getBursementId());
            aoaDetailsburseDaoOfJian.insert(detail);
            System.out.println("3333成功");
        }
    }

    @Override
    public void insertEvec(AoaBursementOfJian aoaBursementOfJian, long proFileId,
                           Long userId, Long typeId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //在总表添加记录
        AoaProcessListOfJian processId = aoaBursementOfJian.getProcessId();
        processId.setApplyTime(sdf.format(new Date()));
        processId.setStatusId(23L);
        if(proFileId != 0L){
            processId.setProFileId(proFileId);
        }
        processId.setProcessUserId(userId);
        processId.setIsChecked(0);
        processId.setShenuser(aoaBursementOfJian.getUsernameshen());
        aoaProcessListDaoOfJian.insert(processId);
        System.out.println("111111111成功");
        //审核表添加字段
        AoaReviewedOfJian aoaReviewedOfJian = new AoaReviewedOfJian();
        aoaReviewedOfJian.setStatusId(23L);
        aoaReviewedOfJian.setDel(0);
        aoaReviewedOfJian.setProId(processId.getProcessId());
        String[] split = aoaBursementOfJian.getUsernameshen().split(";");
        for (String s : split) {
            aoaReviewedOfJian.setUserId(aoaUserDao.selectOne(new QueryWrapper<AoaUser>().eq("user_name",s)).getUserId());
            aoaReviewedDaoOfJian.insert(aoaReviewedOfJian);
        }
        // 添加出差表记录
        AoaEvectionOfJian aoaEvectionOfJian = new AoaEvectionOfJian();
        aoaEvectionOfJian.setTypeId(typeId);
        aoaEvectionOfJian.setProId(processId.getProcessId());
        aoaEvectionDaoOfJian.insert(aoaEvectionOfJian);
        System.out.println("222222222成功");
    }

    @Override
    public void insertOver(AoaBursementOfJian aoaBursementOfJian, Long userId, Long typeId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //在总表添加记录
        AoaProcessListOfJian processId = aoaBursementOfJian.getProcessId();
        processId.setApplyTime(sdf.format(new Date()));
        processId.setStatusId(23L);
        processId.setProcessUserId(userId);
        processId.setIsChecked(0);
        processId.setShenuser(aoaBursementOfJian.getUsernameshen());
        aoaProcessListDaoOfJian.insert(processId);
        System.out.println("111111111成功");
        //审核表添加字段
        AoaReviewedOfJian aoaReviewedOfJian = new AoaReviewedOfJian();
        aoaReviewedOfJian.setStatusId(23L);
        aoaReviewedOfJian.setDel(0);
        aoaReviewedOfJian.setProId(processId.getProcessId());
        String[] split = aoaBursementOfJian.getUsernameshen().split(";");
        for (String s : split) {
            aoaReviewedOfJian.setUserId(aoaUserDao.selectOne(new QueryWrapper<AoaUser>().eq("user_name",s)).getUserId());
            aoaReviewedDaoOfJian.insert(aoaReviewedOfJian);
        }
        // 添加overTime表记录
        AoaOvertimeOfJian aoaOvertimeOfJian = new AoaOvertimeOfJian();
        aoaOvertimeOfJian.setTypeId(typeId);
        aoaOvertimeOfJian.setProId(processId.getProcessId());
        aoaOvertimeDaoOfJian.insert(aoaOvertimeOfJian);
        System.out.println("222222222成功");
    }

    @Override
    public void insertRegu(AoaBursementOfJian aoaBursementOfJian, AoaRegularOfJian aoaRegularOfJian, Long userId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //在总表添加记录
        AoaProcessListOfJian processId = aoaBursementOfJian.getProcessId();
        processId.setApplyTime(sdf.format(new Date()));
        processId.setStatusId(23L);
        processId.setProcessUserId(userId);
        processId.setIsChecked(0);
        processId.setShenuser(aoaBursementOfJian.getUsernameshen());
        aoaProcessListDaoOfJian.insert(processId);
        System.out.println("111111111成功");
        //审核表添加字段
        AoaReviewedOfJian aoaReviewedOfJian = new AoaReviewedOfJian();
        aoaReviewedOfJian.setStatusId(23L);
        aoaReviewedOfJian.setDel(0);
        aoaReviewedOfJian.setProId(processId.getProcessId());
        String[] split = aoaBursementOfJian.getUsernameshen().split(";");
        for (String s : split) {
            aoaReviewedOfJian.setUserId(aoaUserDao.selectOne(new QueryWrapper<AoaUser>().eq("user_name",s)).getUserId());
            aoaReviewedDaoOfJian.insert(aoaReviewedOfJian);
        }
        // 添加aoa_regular表记录
        aoaRegularOfJian.setProId(processId.getProcessId());
        aoaRegularDaoOfJian.insert(aoaRegularOfJian);
        System.out.println("222222222成功");
    }

    @Override
    public void insertHoli(AoaBursementOfJian aoaBursementOfJian,
                           AoaHolidayOfJian aoaHolidayOfJian,
                           Long proFileId,
                           Long userId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //在总表添加记录
        AoaProcessListOfJian processId = aoaBursementOfJian.getProcessId();
        processId.setApplyTime(sdf.format(new Date()));
        processId.setStatusId(23L);
        if(proFileId != 0L){
            processId.setProFileId(proFileId);
        }
        processId.setProcessUserId(userId);
        processId.setIsChecked(0);
        processId.setShenuser(aoaBursementOfJian.getUsernameshen());
        aoaProcessListDaoOfJian.insert(processId);
        System.out.println("111111111成功");
        // 审核表添加记录
        AoaReviewedOfJian aoaReviewedOfJian = new AoaReviewedOfJian();
        aoaReviewedOfJian.setStatusId(23L);
        aoaReviewedOfJian.setDel(0);
        aoaReviewedOfJian.setProId(processId.getProcessId());
        String[] split = aoaBursementOfJian.getUsernameshen().split(";");
        for (String s : split) {
            aoaReviewedOfJian.setUserId(aoaUserDao.selectOne(new QueryWrapper<AoaUser>().eq("user_name",s)).getUserId());
            aoaReviewedDaoOfJian.insert(aoaReviewedOfJian);
        }
        System.out.println("222222222成功");
        // aoa_holiday表添加记录
        aoaHolidayOfJian.setProId(processId.getProcessId());
        aoaHolidayDaoOfJian.insert(aoaHolidayOfJian);
        System.out.println("333333333成功");
    }

    @Override
    public void insertRes(AoaBursementOfJian aoaBursementOfJian, AoaResignOfJian aoaResignOfJian,
                          String handusername, Long userId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //在总表添加记录
        AoaProcessListOfJian processId = aoaBursementOfJian.getProcessId();
        processId.setApplyTime(sdf.format(new Date()));
        processId.setStatusId(23L);
        processId.setProcessUserId(userId);
        processId.setIsChecked(0);
        processId.setShenuser(aoaBursementOfJian.getUsernameshen());
        aoaProcessListDaoOfJian.insert(processId);
        System.out.println("111111111成功");
        //审核表添加字段
        AoaReviewedOfJian aoaReviewedOfJian = new AoaReviewedOfJian();
        aoaReviewedOfJian.setStatusId(23L);
        aoaReviewedOfJian.setDel(0);
        aoaReviewedOfJian.setProId(processId.getProcessId());
        String[] split = aoaBursementOfJian.getUsernameshen().split(";");
        for (String s : split) {
            aoaReviewedOfJian.setUserId(aoaUserDao.selectOne(new QueryWrapper<AoaUser>().eq("user_name",s)).getUserId());
            aoaReviewedDaoOfJian.insert(aoaReviewedOfJian);
        }
        System.out.println("2222222222成功");
        // aoa_resign表添加记录
        aoaResignOfJian.setHandUser(aoaUserDao.selectOne(new QueryWrapper<AoaUser>().eq("user_name",handusername)).getUserId());
        aoaResignOfJian.setProId(processId.getProcessId());
        aoaResignDaoOfJian.insert(aoaResignOfJian);
        System.out.println("333333333成功");
    }

}
